import pandas as pd
import numpy as np
import pandas as pd
from keras import callbacks
from keras.models import load_model
import keras
from keras import backend as K
import keras.optimizers as opt
from keras import Input,layers
from keras.models import Model
from keras import regularizers
callback_list = [
        callbacks.EarlyStopping(monitor="val_loss", patience=5),
        callbacks.ModelCheckpoint(filepath="model_1.h5", monitor="val_loss", save_best_only=True),
        callbacks.ReduceLROnPlateau(monitor="val_loss", factor=0.5, verbose=1, patience=1)
    ]
data=np.array(pd.read_csv("data_1.csv",names=['prov_id', 'area_id', 'chnl_type', 'service_type', 'product_type', 'innet_months', 'total_times', 'total_flux', 'total_fee', 'pay_fee', 'sex', 'age', 'manu_name', 'term_type', 'max_rat_flag', 'is_5g_base_cover', 'is_work_5g_cover', 'is_home_5g_cover', 'is_work_5g_cover_l01', 'is_home_5g_cover_l01', 'is_work_5g_cover_l02', 'is_home_5g_cover_l02', 'activity_type', 'is_act_expire', 'comp_type', 'call_days', 're_call10', 'short_call10', 'long_call10', 'bank_cnt', 'game_app_flux', 'live_app_flux', 'video_app_flux', 'city_5g_ratio', 'city_level', 'app_sum']))
label=np.array(pd.read_csv("label_1.csv"))
data=data[1:,:]
def f1(y_true, y_pred):
    def recall(y_true, y_pred):
        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
        possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
        recall = true_positives / (possible_positives + K.epsilon())
        return recall
    def precision(y_true, y_pred):
        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
        predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
        precision = true_positives / (predicted_positives + K.epsilon())
        return precision
    precision = precision(y_true, y_pred)
    recall = recall(y_true, y_pred)
    return 2 * ((precision * recall) / (precision + recall + K.epsilon()))

data_test=np.array(pd.read_csv("data_test_1.csv",names=['prov_id', 'area_id', 'chnl_type', 'service_type', 'product_type', 'innet_months', 'total_times', 'total_flux', 'total_fee', 'pay_fee', 'sex', 'age', 'manu_name', 'term_type', 'max_rat_flag', 'is_5g_base_cover', 'is_work_5g_cover', 'is_home_5g_cover', 'is_work_5g_cover_l01', 'is_home_5g_cover_l01', 'is_work_5g_cover_l02', 'is_home_5g_cover_l02', 'activity_type', 'is_act_expire', 'comp_type', 'call_days', 're_call10', 'short_call10', 'long_call10', 'bank_cnt', 'game_app_flux', 'live_app_flux', 'video_app_flux', 'city_5g_ratio', 'city_level', 'app_sum']))
label_test=np.array(pd.read_csv("label_test_1.csv"))
data_test=data_test[1:,:]

data_input = Input(shape=(36,))

x = layers.Dense(64, activation="relu")(data_input)
x = layers.normalization.BatchNormalization()(x)
x=layers.Dropout(0.1)(x)
x= layers.Dense(512, activation="relu")(x)
x = layers.normalization.BatchNormalization()(x)
x=layers.Dropout(0.1)(x)
x= layers.Dense(128, activation="relu")(x)
x = layers.normalization.BatchNormalization()(x)
x=layers.Dropout(0.1)(x)



y= layers.Dense(64, activation="relu")(data_input)
y = layers.normalization.BatchNormalization()(y)
y=layers.Dropout(0.1)(y)
y= layers.Dense(256, activation="relu")(y)
y = layers.normalization.BatchNormalization()(y)
y=layers.Dropout(0.1)(y)
y= layers.Dense(128, activation="relu")(y)
y = layers.normalization.BatchNormalization()(y)
y=layers.Dropout(0.1)(y)




x=layers.concatenate([x,y],axis=1)

x = layers.Dense(32, activation="relu")(x)
x=layers.Dropout(0.1)(x)
x = layers.Dense(1,activation="sigmoid")(x)
model_1 = Model(inputs=data_input, outputs=x)
model_1.compile(optimizer=opt.adam(), loss="binary_crossentropy",metrics=[f1,"acc"])
model_1.fit(data,label, epochs=12, batch_size=4096, callbacks=callback_list,validation_data=(data_test,label_test))
